﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace ViCode_LeVi.Data
{
    /// <summary>
    /// Có danh sách con (type là chính nó)
    /// </summary>
    [DataContract]
    public class Step_Budget : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members
        public void SendPropertyChanged(string sProp)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(sProp));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
        //StepInfo_Budget[] Childs
        //Name (ten), Cost  (chi phi), Note (ghi chu)

        #region Note
        string _Note;
        [DataMember]
        public string Note
        {
            get { return _Note; }
            set
            {
                if (_Note != value)
                {
                    _Note = value;
                    SendPropertyChanged("Note");
                }
            }
        }
        #endregion

        #region Name
        string _Name;
        [DataMember]
        public string Name
        {
            get { return _Name; }
            set
            {
                if (_Name != value)
                {
                    _Name = value;
                    SendPropertyChanged("Name");
                }
            }
        }
        #endregion

        #region Cost
        double _Cost;
        [DataMember]
        public double Cost
        {
            get { return _Cost; }
            set
            {
                if (_Cost != value)
                {
                    _Cost = value;
                    SendPropertyChanged("Cost");
                    SendPropertyChanged("Cost_Childs");
                    SendPropertyChanged("Get_Cost");
                    SendPropertyChanged("CostCurrent");
                    if (Parent != null)
                    {
                        Parent.SendPropertyChanged("Cost_Childs");
                        Parent.SendPropertyChanged("Get_Cost");
                        Parent.SendPropertyChanged("CostCurrent");
                    }

                }
            }
        }

        public double CostCurrent
        {
            get
            {
                if (Childs != null && Childs.Count > 0)
                {
                    //tinh lai cost
                    if (Cost != CostTotalChild)
                    {
                        Cost = CostTotalChild;
                    }
                    return CostTotalChild;
                }
                return Cost;
            }
        }

        public double CostTotalChild
        {
            get
            {
                return Childs.Sum(c => c.CostCurrent);
            }
            set
            { }
        }
        public double CostPercentWithParent
        {
            get
            {
                if (Parent == null)
                    return 100;
                return CostCurrent * 100 / Parent.CostTotalChild;
            }
            set { }
        }
        #endregion

        #region Parent
        Step_Budget _Parent;
        public Step_Budget Parent
        {
            get
            {
                return _Parent;
            }
            set
            {
                if (_Parent != value)
                {
                    _Parent = value;
                    SendPropertyChanged("Parent");
                }
            }
        }
        public void RefreshParent()
        {
            foreach (var c in Childs)
            {
                if (c.Parent == null)
                {
                    c.Parent = this;
                    c.RefreshParent();
                }
            }
        }
        public bool CanEditCost
        {
            get { return this.Childs == null || this.Childs.Count == 0; }
        }
        #endregion

        #region Childs
        List<Step_Budget> _Childs;
        [DataMember]
        public List<Step_Budget> Childs
        {
            get
            {
                if (_Childs == null)
                    _Childs = new List<Step_Budget>();
                return _Childs;
            }
            set
            {
                if (_Childs != value)
                {
                    _Childs = value;
                    SendPropertyChanged("Childs");
                    SendPropertyChanged("ObservableCollection_Childs");
                }
            }
        }
        //ObservableCollection<Step_Budget> _ObservableCollection_Childs;
        public ObservableCollection<Step_Budget> ObservableCollection_Childs
        {
            get
            {
                return new ObservableCollection<Step_Budget>(Childs);
            }
        }

        public double Cost_Childs
        {
            get
            {
                if (Childs != null && Childs.Count > 0)
                {
                    return Childs.Sum(c => c.Cost_Childs);
                }
                else
                    return Cost;

            }
        }
        public double Get_Cost
        {
            get
            {
                double result = Cost;
                if (Childs != null && Childs.Count > 0)
                {
                    result = Childs.Sum(c => c.Cost_Childs);
                }
                if (Parent != null)
                {
                    Parent.SendPropertyChanged("Cost_Childs");
                    Parent.SendPropertyChanged("Get_Cost");
                }
                return result;
            }
        }
        #endregion

        #region Instance

        #endregion

        #region Action
        public void Add_Budget(Step_Budget sb)
        {
            if (sb == null)
                return;
            sb.Parent = this;
            sb.IsSelected = true;
            Childs.Add(sb);
            this.IsExpanded = true;
            SendPropertyChanged("Childs");
            SendPropertyChanged("ObservableCollection_Childs");
        }
        public void Remove_BudgetSub(Step_Budget sb)
        {
            if (sb == null)
                return;
            if (Childs.Contains(sb))
                Childs.Remove(sb);
            SendPropertyChanged("Childs");
            SendPropertyChanged("ObservableCollection_Childs");
        }
        #endregion

        #region IsExpanded
        private bool _isExpanded;
        public bool IsExpanded
        {
            get
            {
                return _isExpanded;
            }

            set
            {
                _isExpanded = value;
                this.SendPropertyChanged("IsExpanded");
            }
        }
        #endregion

        #region IsSelected
        private bool _isSelected;
        public bool IsSelected
        {
            get
            {
                return _isSelected;
            }

            set
            {
                _isSelected = value;
                this.SendPropertyChanged("IsSelected");
            }
        }

        #endregion
    }
}
